/*
 * Copyright 2009-2010 Belmont Software Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.belmont.backup.client;

import java.util.*;
import com.google.gwt.core.client.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.*;

public class AboutPanel extends UIPanel {
    HTML backupDirectoryInfoField = new HTML();
    Label versionLabel = new Label("Undetermined");
    Label backupHostField = new Label();
    Label backupIPField = new Label();
    Label backupPortField = new Label();
    Label startedOnField = new Label();
    Label statusField = new Label();
    Label logDirField = new Label();

    public AboutPanel(BackupUI main) {
        super(main, "About");
    }

    public void refresh() {
        main.service.getBrandingStrings(new AsyncCallback<String[]>() {
                public void onFailure(Throwable caught) {
                    main.print("Failed to get branding strings, using default.");
                }
                public void onSuccess(String result[]) {
                    initHelpMsg.setHTML("This is the admin panel for the <b>"+result[BackupUI.BRANDING_COMPANY]+" "+result[BackupUI.BRANDING_PRODUCT]+"</b>. Status about the server is listed below.");
                }
            });

        main.service.isServiceEnabled(new AsyncCallback<Boolean>() {
                public void onFailure(Throwable caught) {
                    main.print("Communication with backup server failed.");
                }
                public void onSuccess(Boolean result) {
                    statusField.setText(result ? "Enabled" : "Disabled");
                }
            });

        main.service.getBackupRoot(new AsyncCallback() {
                public void onFailure(Throwable caught) {
                    main.print("Communication with backup server failed.");
                }
                public void onSuccess(Object result) {
                    backupDirectoryInfoField.setText((String)result);
                }
            });

        main.service.getVersion(new AsyncCallback() {
                public void onFailure(Throwable caught) {
                    versionLabel.setText("?");
                    main.print("Communication with backup server failed.");
                }
                public void onSuccess(Object result) {
                    versionLabel.setText((String)result);
                }
            });


        main.service.getServiceHost(new AsyncCallback() {
                public void onFailure(Throwable caught) {
                    main.print("Communication with backup server failed.");
                }
                public void onSuccess(Object result) {
                    backupHostField.setText((String)result);
                }
            });
        main.service.getServiceIP(new AsyncCallback() {
                public void onFailure(Throwable caught) {
                    main.print("Communication with backup server failed.");
                }
                public void onSuccess(Object result) {
                    backupIPField.setText((String)result);
                }
            });
        main.service.getServicePort(new AsyncCallback() {
                public void onFailure(Throwable caught) {
                    main.print("Communication with backup server failed.");
                }
                public void onSuccess(Object result) {
                    backupPortField.setText((String)result);
                }
            });

        main.service.getStartedOn(new AsyncCallback() {
                public void onFailure(Throwable caught) {
                    main.print("Communication with backup server failed.");
                }
                public void onSuccess(Object result) {
                    startedOnField.setText((String)result);
                }
            });
        main.service.getLogDirectory(new AsyncCallback<String>() {
                public void onFailure(Throwable caught) {
                    main.print("Communication with backup server failed.");
                }
                public void onSuccess(String result) {
                    logDirField.setText(result);
                }
            });
    }

    HTML initHelpMsg;

    public Widget init() {
        DockPanel dp = new DockPanel();
        Grid g = new Grid(8, 3);
        HTMLTable.RowFormatter rf = g.getRowFormatter();
        initHelpMsg = new HTML("This is the admin panel for the <b>Belmont Backup Service</b>. Information about this server is listed below.");

        dp.add(initHelpMsg, DockPanel.NORTH);
        initHelpMsg.setStyleName("backup-Help");

        g.setCellPadding(4);

        versionLabel.setHorizontalAlignment(Label.ALIGN_LEFT);
        addRowToControlPanel(g, 0,"Version:", versionLabel,"Version number for the backup service.");
        addRowToControlPanel(g, 1,"Backup Directory:",backupDirectoryInfoField,
                             "The directory the service uses to store backups for all clients.");
        addRowToControlPanel(g, 2,"Log Directory:",logDirField,
                             "The directory the service uses to record activity and error logs.");
        addRowToControlPanel(g, 3,"Host:",backupHostField, "The host running the service.");
        addRowToControlPanel(g, 4,"IP Address:",backupIPField, "The host running the service.");
        addRowToControlPanel(g, 5,"Port:",backupPortField, "The port the service uses for administration.");
        addRowToControlPanel(g, 6,"Started On:",startedOnField, "The date/time on which this service was started.");
        addRowToControlPanel(g, 7,"Status:",statusField, "Whether this service is enabled or not.");

        DecoratorPanel dec = new DecoratorPanel();
        dec.add(g);
        dp.add(dec, DockPanel.CENTER);
        dp.setCellHorizontalAlignment(dec, HasHorizontalAlignment.ALIGN_CENTER);
        dp.setCellVerticalAlignment(dec, HasVerticalAlignment.ALIGN_TOP);
        dp.setHeight("100%");
        dp.setWidth("90%");

        return dp;
    }

}